const parser = require("@babel/parser");
const template = require("@babel/template").default;
const traverse = require("@babel/traverse").default;
const types= require("@babel/types");
const generator = require("@babel/generator").default;
const fs = require('fs');
var content =fs.readFileSync("./source.js",{ "encoding": 'utf-8' })
var ast =parser.parse(content)
function getLastConsequent(consequents) {
 let len = consequents.length;
 if (types.isBreakStatement(consequents[len-1]))
 {
    consequents.pop();
 }
 return consequents;
}

function getSwitchNodes(scope,name) {
   let switchNodes = [];
   scope.traverse(scope.block,{
      SwitchStatement({node})
      {
         let {discriminant,cases} = node;
         if (discriminant.name != name) return;
         for (let i = 0; i < cases.length;i++)
         {
            let consequent = cases[i].consequent;
            if (types.isSwitchStatement(consequent[0]))
            {
               switchNodes[i] = [];
               let secondCases = consequent[0].cases;
               for (let j=0;j<secondCases.length;j++)
               {
                  let consequent = secondCases[j].consequent;
                  switchNodes[i][j] = getLastConsequent(consequent);
               }
            }
            else
            {
               switchNodes[i] = getLastConsequent(cases[i].consequent)
            }
         }
      },
   })

   return switchNodes;
}
function getItems(switchNodes,initValue) {
   var a = 7 & initValue, s = initValue >> 3, r = 7 & s;

   let items = switchNodes[a];//一维数组取值
   if (r < switchNodes[a].length && Array.isArray(switchNodes[a][r]))
   {//二维数组取值
      items = switchNodes[a][r];
   }
   return items;
}
const visitor={
    ForStatement(path)
   {
      let {init,test,body} = path.node;
      if (!types.isVariableDeclaration(init)) return;
      if (!body.body || body.body.length != 2) return;
      if (init.declarations.length != 1) return;
      let initName = init.declarations[0].id.name;
      let initValue = init.declarations[0].init.value;
      switchNodes = getSwitchNodes(path.scope,'a');
      let items = getItems(switchNodes,2);
      for (const item of items)
      {
         console.log(generator(item).code);
      }
   },
}

traverse(ast,visitor)
let { code } = generator(ast);
console.log(code);